package com.itheima.controller;


import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionServicen;

    /**
     * 分页
     * @author Qifei
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
         return permissionServicen.findPage(queryPageBean);
    }

    /**
     * 新增
     * @author Qifei
     * @param permission
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        permissionServicen.add(permission);
        return Result.success("添加成功");
    }

    /**
     * 编辑回显
     * @author Qifei
     * @param id
     * @return
     */
    @RequestMapping("/findPermissionById")
    public Result findPermissionById(Integer id){
        Permission permission = permissionServicen.findPermissionById(id);
        return Result.success("",permission);
    }

    /**
     * 编辑
     * @author Qifei
     * @param permission
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try {
            permissionServicen.edit(permission);
            return Result.success("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("编辑失败");
        }
    }

    /**
     * 删除
     * @author Qifei
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            permissionServicen.delete(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("此条记录存在引用，不能删除");
        }
    }



}
